﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestWebApplication.CommerceTransaction;
using TestWebApplication.CommerceTestDataProvider;
using Microsoft.Commerce.Proxy.TransactionService.v201001;
using TestWebApplication.CommercePaymentInstrument;
using CommercePIProxy = Microsoft.Commerce.Proxy.PaymentInstrumentService;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;

namespace TestWebApplication
{
    public partial class Purchase1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            itemID.Text = Request["itemID"];

            PurchaseInfoInput purchaseInfo = (PurchaseInfoInput)Session["purchaseInfo"];
            title.Text = purchaseInfo.PurchaseBundleInputSet[0].Title;
            amount.Text = purchaseInfo.PurchaseBundleInputSet[0].Amount.ToString();

            piid.Text = Request["piid"];
           // piid.Text = "mDoAAAAAAAABAACA";
            if (String.IsNullOrEmpty(piid.Text)) piid.Text = "You have not chosen a Payment Instrument";
            
        }

        protected void buy_Click(object sender, EventArgs e)
        {
            string accountID = (string)Session["accountID"];
            string pid = piid.Text;
            /*if (pid.Equals("You have not chosen a Payment Instrument"))
            {
                string brazil = brazilPI.SelectedValue;
                if (brazil.Equals("Boleto")) pid = Constants.TestBoletoPIID;
                else if (brazil.Equals("Online Bank Transfer(Banco do Brasil)")) pid = Constants.TestOnlineBankBancodoBrasilPIID;
                else if (brazil.Equals("Online Bank Transfer(Bradesco)")) pid = Constants.TestOnlineBankBradescoPIID;
                else if (brazil.Equals("Online Bank Transfer(Itau)")) pid = Constants.TestOnlineBankItauPIIDBradesco;
                else if (brazil.Equals("Mercado Pago")) pid = Constants.TestMercadoPagoPIID;
            }*/

            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];
            PurchaseInfoInput purchaseInfo = (PurchaseInfoInput)Session["purchaseInfo"];

            string surl = "s.htm";
            string furl = "f.htm";
            PurchaseOutput purchaseResult = PurchaseHandler.purchase(pid, accountID, surl, furl, requester, purchaseInfo);
            //Response.Write(purchaseResult.PropertyBag.Length);

            string url = PurchaseHandler.purchaseRedirect(purchaseResult);
            Session["html"] = url;
            Response.Redirect("PurchaseSuccess.aspx");

        }

        protected void addPI_Click(object sender, EventArgs e)
        {
            string itemID = Request["itemID"];
            string locale = (string)Session["locale"];
            string accountID = (string)Session["accountID"];

            string url = PCSHandler.generatePCSUrl(locale, itemID, accountID);

            Response.Redirect(url);
        }
    }
}